﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kompetition
{
    public partial class Hub : Form
    {
        Competition competition = null;
        string competitionPath = null;
        Font title, text;
        double width, height;

        public Hub()
        {
            InitializeComponent();
        }

        private void panelQuestions_Paint(object sender, PaintEventArgs e)
        {
            if (competition == null)
                return;
            int mostQ = 0;
            foreach (var amount in competition.Categories)
            {
                if (amount.Questions.Length > mostQ)
                    mostQ = amount.Questions.Length;
            }
            mostQ++;

            height = (double)e.ClipRectangle.Height / mostQ;
            width = (double)e.ClipRectangle.Width / competition.Categories.Count;
            int size = (int)Math.Floor(height > width ? width : height);
            title = new Font(new Font(new FontFamily("Times New Roman"), size / 3 * 2, FontStyle.Bold), FontStyle.Bold);
            text = new Font(new Font(new FontFamily("Courier New"), size / 2, FontStyle.Regular), FontStyle.Regular);

            for (int i = 0; i < competition.Categories.Count; i++)
            {
                e.Graphics.DrawString(competition.Categories[i].Name, title, Brushes.Green, new PointF((float)width * i, 0));
                for (int j = 0; j < competition.Categories[i].Questions.Length; j++)
                {
                    Rectangle region = new Rectangle((int)width * i, (int)height * (j + 1), (int)width, (int)height);
                    Image toDraw = Properties.Resources.buttonTexturePurpleMesh;
                    if (competition.Categories[i].IsQuestionAnswered(competition.Categories[i].Questions[j]))
                        toDraw = Properties.Resources.buttonTextureWhiteMarble;
                    e.Graphics.DrawImage(toDraw, region);

                    string text2 = competition.Categories[i].GetPointValue(competition.Categories[i].Questions[j]).ToString();
                    e.Graphics.DrawString(text2, text, Brushes.Black,
                        new PointF((float)width * i + (float)width / 2 - size / 2 * text2.Length, (float)height * (j + 1) + 5));
                }
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = "*.ktn";
            dialog.Filter = "Kompetitions (*.ktn)|*.ktn";
            dialog.ShowDialog();
            if (dialog.FileName != null && System.IO.File.Exists(dialog.FileName))
            {
                competitionPath = dialog.FileName;
                competition = new Competition(competitionPath);
                panelQuestions.Invalidate();
                RefreshScores();
            }
        }

        private void panelQuestions_MouseClick(object sender, MouseEventArgs e)
        {
            if (competition == null)
                return;
            if (e.Y < height)
                return;
            int cNum = (int)(e.X - e.X % (int)width) / (int)width,
                qNum = (int)(e.Y - e.Y % (int)height) / (int)height - 1;
            new ShowQuestion(competition.Categories[cNum].Questions[qNum], competition.Categories[cNum], competition).ShowDialog();
            panelQuestions.Invalidate();
            RefreshScores();
            if (competition.IsGameOver)
                MessageBox.Show(competition.TeamAt(competition.DetermineWinningTeam()).Key + " won the competition!", "Kompetition");
        }

        private void RefreshScores()
        {
            labelScoresDisp.Text = "Team Scores: \r\n";
            foreach (var team in competition.Teams)
                labelScoresDisp.Text += team.Key.ToUpper() + ": " + team.Value.ToString() + "\r\n";
            labelCurrentTeam.Text = "Current Team: \r\n" + competition.TeamAt(competition.CurrentTeamNumber).Key.ToUpper();
        }

        /*private void buttonNewGame_Click(object sender, EventArgs e)
        {
            if (competitionPath == null)
            {
                MessageBox.Show("You must load a Kompetition file before starting a game.", "Kompetition");
                return;
            }
            competition = new Competition(competitionPath);
            panelQuestions.Invalidate();
        }

        private void buttonEndGame_Click(object sender, EventArgs e)
        {
            if (competition == null)
            {
                MessageBox.Show("You must start a game before you can end it.", "Kompetition");
                return;
            }
            competition = null;
            panelQuestions.Invalidate();
        }*/
    }
}
